package com.work.trans.controller;

import com.work.trans.service.UserService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:DELL
 * Date:2025-06-11
 * Time:22:58
 */
@RequestMapping("/trans")
@RestController
public class TransController {
    @Autowired
    private UserService userService;

    //对事务进行了捕获，事务提交
    //对事务未进行捕获，会回滚
    @Transactional
    @RequestMapping("/r1")
    public Boolean register(String userName, String password){
        Integer result = userService.insert(userName, password);
        System.out.println("插入用户表, result: "+ result);
        try {
            int a = 10/0;
        }catch (Exception e){
            e.printStackTrace();
        }

        return true;
    }

    /**
     * 重新把异常抛出去, 事务回滚
     */
    @Transactional
    @RequestMapping("/r2")
    public Boolean r2(String userName, String password){
        Integer result = userService.insert(userName, password);
        System.out.println("插入用户表, result: "+ result);
        try {
            int a = 10/0;
        }catch (Exception e){
            throw e;
        }

        return true;
    }

    //手动把事务回滚
    @Transactional
    @RequestMapping("/r3")
    public Boolean r3(String userName, String password){
        Integer result = userService.insert(userName, password);
        System.out.println("插入用户表, result: "+ result);
        try {
            int a = 10/0;
        }catch (Exception e){
            //手动回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }

        return true;
    }

    //事务提交
    @Transactional
    @RequestMapping("/r4")
    public Boolean r4(String userName, String password) throws IOException {
        Integer result = userService.insert(userName, password);
        System.out.println("插入用户表, result: "+ result);
        if(true){
            throw new IOException();
        }

        return true;
    }

    //事务回滚
    @SneakyThrows//lombok的注解
    @Transactional
    @RequestMapping("/r5")
    public Boolean r5(String userName, String password) {
        Integer result = userService.insert(userName, password);
        System.out.println("插入用户表, result: "+ result);
        if(true){
            throw new IOException();
        }
        return true;
    }

    //事务回滚
    @Transactional
    @RequestMapping("/r6")
    public Boolean r6(String userName, String password) {
        Integer result = userService.insert(userName, password);
        System.out.println("插入用户表, result: "+ result);
        if(true){
            throw new RuntimeException();
        }
        return true;
    }
    //事务回滚。
    @Transactional(rollbackFor = Exception.class,isolation = Isolation.DEFAULT)
    @RequestMapping("/r7")
    public Boolean r7(String userName, String password) throws IOException {
        Integer result = userService.insert(userName, password);
        System.out.println("插入用户表, result: "+ result);
        if(true){
            throw new IOException();
        }
        return true;
    }
}
